package cn.school.workflow.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.school.common.utils.SysUserUtils;
import cn.school.sys.dao.SysUserDao;
import cn.school.workflow.dao.UsePersonApplyDao;
import cn.school.workflow.service.UsePersonApplyService;
import cn.school.workflow.service.WorkFlowCommonService;
import cn.school.workflow.vo.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UsePersonApplyServiceImpl extends ServiceImpl<UsePersonApplyDao, UsePersonApplyVO> implements UsePersonApplyService {

    @Autowired
    private WorkFlowCommonService workFlowCommonService;
    @Resource
    private SysUserDao sysUserDao;

    @Override
    public IPage<UsePersonApplyVO> findUsePersonApplyPageList(UsePersonApplyVO vo) {
        LambdaQueryWrapper<UsePersonApplyVO> wrapper = new LambdaQueryWrapper<>(vo);
        //添加租户条件
        wrapper.eq(UsePersonApplyVO::getTenantId,vo.getTenantId());

        if(!SysUserUtils.getSysUser().getDeptName().startsWith("人事")){
            //默认查询自己提交的申请
            wrapper.eq(UsePersonApplyVO::getCreateBy,SysUserUtils.getSysUser().getUsername());
        }
        if(!StringUtils.isEmpty(vo.getApplyNo())){
            wrapper.eq(UsePersonApplyVO::getApplyNo,vo.getApplyNo());
        }
        wrapper.orderByDesc(UsePersonApplyVO::getCreateTime);
        return page(new Page<>(vo.getPageNum(), vo.getPageSize()), wrapper);
    }

    @Override
    public int addUsePersonApply(UsePersonApplyVO vo) {

        List<String> list = sysUserDao.findDeptLeaderApprovalUserByDeptName("分管校长");
        if(list==null || list.size()==0){
            //找不到下级审批人
            return -1;
        }
        String username = SysUserUtils.getSysUser().getUsername();
        String applyNo = "UP_"+ DateUtil.format(new Date(),"yyyyMMddHHmmssSSS");
        vo.setCurrApprovedBy(list.get(0));
        vo.setApplyNo(applyNo);
        vo.setCreateBy(username);
        vo.setCreateTime(new Date());
        vo.setStatus("1");
        //创建申请单
        this.baseMapper.insert(vo);

        WorkFlowVO flowVO = new WorkFlowVO();
        flowVO.setApplyTitle("部门用人申请");
        flowVO.setApplyID("usePersonApply");
        flowVO.setBusinessKey(applyNo);
        //查询下级处理人
        Map<String,Object> map =new HashMap<>();
        map.put("assignee",list.get(0));
        flowVO.setVariables(map);
        //创建请假工作流
        String processInstanceId = workFlowCommonService.createProcessInstance(flowVO);
        vo.setProcessId(processInstanceId);
        //更新流程实例ID
        return this.baseMapper.updateById(vo);

    }

    @Override
    public boolean cancelUsePersonApply(WorkFlowVO vo) {
        LambdaQueryWrapper<UsePersonApplyVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UsePersonApplyVO::getProcessId,vo.getProcessInstanceId());
        UsePersonApplyVO applyVO = new UsePersonApplyVO();
        applyVO.setStatus("4");//撤销流程
        this.baseMapper.update(applyVO,wrapper);
        return workFlowCommonService.cancelApply(vo.getProcessInstanceId(),vo.getApplyDesc());
    }

    @Override
    public UsePersonApplyVO findUsePersonApplyDetail(Long processInstanceId) {
        LambdaQueryWrapper<UsePersonApplyVO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UsePersonApplyVO::getProcessId,processInstanceId);
        return this.baseMapper.selectOne(wrapper);
    }
}
